package stations.QuakeInfo;

import java.util.List;

import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class StationsQuakeInfoActivity extends MapActivity  {
    /** Called when the activity is first created. */
	MapView mapView; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
        String xmlContent;
        StringBuffer stringBuffer = new StringBuffer();
        XmlResourceParser xmlparser = this.getResources().getXml(R.xml.qstations);
        List earthquakesList= EarthquakeXmltoBO.getEarthquakeData(xmlparser);

		xmlContent = stringBuffer.toString();
		mapView = (MapView) findViewById(R.id.mapView);
	    mapView.setBuiltInZoomControls(true);
	    
        MapController mapController = mapView.getController();

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
        MapCustomizedOverlay itemizedOverlay = new MapCustomizedOverlay(drawable, this);
        GeoPoint p;        
        for(int i=0; i<earthquakesList.size(); i++){
        	EarthQuakes quakes = (EarthQuakes)earthquakesList.get(i);
        	double mlat = quakes.mapCenter.latitude;
        	double mlng = quakes.mapCenter.longitude;
        	p = new GeoPoint((int) (mlat * 1E6),(int) (mlng * 1E6));
            mapController.animateTo(p);
        	List earthquakeObjects = quakes.getEarthQuakes();
        	for (int j = 0; j < earthquakeObjects.size(); j++) {
				Earthquake quake = (Earthquake)earthquakeObjects.get(j);
				List stations = quake.getStations();
				for (int k = 0; k < stations.size(); k++) {
					Station station = (Station)stations.get(k);
					OverlayItem ovl = new OverlayItem( new GeoPoint((int)(station.getLatitude()*1000000),
		        			(int)(station.getLongitude()*1000000)), station.getName(),Double.toString(station.getDistance()));
		           	itemizedOverlay.addOverlay(ovl); 
				}
			}
        } 
   

        mapController.setZoom(6); 
        mapOverlays.add(itemizedOverlay);
        mapView.postInvalidate();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}